package leesoft.toolbox;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.collections.ExtendedProperties;

public class PropertyManager {

	private static PropertyManager instance;
	private final static String[] PROP_FILE_NAMES=new String[]{"/art.properties"};
	private ExtendedProperties extProperties=null;
	
	private PropertyManager(){
		for(int i=0;i<PROP_FILE_NAMES.length;i++){
			ExtendedProperties defaultProperties = loadDefaultProperties(PROP_FILE_NAMES[i]);
			if(defaultProperties!=null){
				if(extProperties==null)
					extProperties=defaultProperties;	
				else
					extProperties.putAll(defaultProperties);
			}
		}
		
	}
	private ExtendedProperties loadDefaultProperties(String filename)
	{
		 InputStream inputStream = null;
	     ExtendedProperties defaultProperties = new ExtendedProperties();

	     try
	     {
	    	 inputStream = getClass()
	                    .getResourceAsStream(filename);
	    	
	    	 
	         if (inputStream != null)
	         {
	        	 defaultProperties.load(inputStream);
	         }
	        }
	        catch (IOException ioe)
	        {
	        	Logger.global.log(Level.SEVERE, "Can't load the app configuration");
	        }
	        finally
	        {
	            try
	            {
	                if (inputStream != null)
	                {
	                    inputStream.close();
	                }
	            }
	            catch (IOException ioe)
	            {
	            	Logger.global.log(Level.SEVERE, "Can't close the app configuration");
	            }
	        }
	        return defaultProperties;
	}
	
	public static synchronized PropertyManager getInstance()
	{
		if(instance==null)
		{
			instance=new PropertyManager();
		}
		return instance;
	}
	public String getValue(String key)
	{
		return extProperties.getString(key);
	}
	public List<String> getListValue(String key){
		ArrayList<String> result=new ArrayList<String>();
		int totalGroupsNumber=extProperties.getInt(key);
		for(int i=0;i<totalGroupsNumber;i++)
		{
			result.add(extProperties.getString(key+i));
		}
		return result;
	}
}
